import { pathOr } from 'ramda';
import initServerInstance from '../server/initServerInstance';

export async function addCountEnable({ balance, model }) {
	balance.balance.domain.used = await model.Job.count({
		uid: balance.uid,
	});
	balance.balance.domainEnable.used = await model.Job.count({
		uid: balance.uid,
		enableMonitor: true,
		'verification.isVerified': true,
	});

	const getCount = pathOr(0, [0, 'count']);

	balance.balance.httpMaxTask.used = getCount(
		await model.Job.aggregate([
			{
				$match: {
					uid: balance.uid,
					'verification.isVerified': true,
				},
			},
			{
				$group: { _id: 'http', count: { $sum: { $size: '$httpSettings' } } },
			},
		])
	);
	if (
		balance.balance.httpMaxTask.used > 0 &&
		balance.balance.httpMaxTask.charges &&
		balance.balance.httpMaxTask.charges.length > 0
	) {
		balance.balance.httpMaxTask.charges[0].used =
			balance.balance.httpMaxTask.used;
	}

	balance.balance.pingMaxTask.used = getCount(
		await model.Job.aggregate([
			{
				$match: {
					uid: balance.uid,
					'verification.isVerified': true,
				},
			},
			{
				$group: { _id: 'ping', count: { $sum: { $size: '$pingSettings' } } },
			},
		])
	);
	if (
		balance.balance.pingMaxTask.used > 0 &&
		balance.balance.pingMaxTask.charges &&
		balance.balance.pingMaxTask.charges.length > 0
	) {
		balance.balance.pingMaxTask.charges[0].used =
			balance.balance.pingMaxTask.used;
	}

	balance.balance.httpMaxTaskEnable.used = getCount(
		await model.Job.aggregate([
			{
				$match: {
					uid: balance.uid,
					enableMonitor: true,
					'verification.isVerified': true,
				},
			},
			{ $unwind: '$httpSettings' },
			{ $match: { 'httpSettings.enable': true } },
			{
				$group: { _id: 'httpEnable', count: { $sum: 1 } },
			},
		])
	);
	if (
		balance.balance.httpMaxTaskEnable.used > 0 &&
		balance.balance.httpMaxTaskEnable.charges &&
		balance.balance.httpMaxTaskEnable.charges.length > 0
	) {
		balance.balance.httpMaxTaskEnable.charges[0].used =
			balance.balance.httpMaxTaskEnable.used;
	}

	balance.balance.pingMaxTaskEnable.used = getCount(
		await model.Job.aggregate([
			{
				$match: {
					uid: balance.uid,
					enableMonitor: true,
					'verification.isVerified': true,
				},
			},
			{ $unwind: '$pingSettings' },
			{ $match: { 'pingSettings.enable': true } },
			{
				$group: { _id: 'pingEnable', count: { $sum: 1 } },
			},
		])
	);
	if (
		balance.balance.pingMaxTaskEnable.used > 0 &&
		balance.balance.pingMaxTaskEnable.charges &&
		balance.balance.pingMaxTaskEnable.charges.length > 0
	) {
		balance.balance.pingMaxTaskEnable.charges[0].used =
			balance.balance.pingMaxTaskEnable.used;
	}

	balance.balance.sitePortrait.used = getCount(
		await model.Job.aggregate([
			{
				$match: {
					uid: balance.uid,
					enableMonitor: true,
					'verification.isVerified': true,
					'vulSettings.enableSitePortraitTriggerMonitor': true,
				},
			},
			{
				$group: {
					_id: 'enableSitePortraitTriggerMonitor',
					count: { $sum: 1 },
				},
			},
		])
	);
	if (
		balance.balance.sitePortrait.used > 0 &&
		balance.balance.sitePortrait.charges &&
		balance.balance.sitePortrait.charges.length > 0
	) {
		balance.balance.sitePortrait.charges[0].used =
			balance.balance.sitePortrait.used;
	}
}

if (require.main === module) {
	(async () => {
		const server = await initServerInstance({
			loggerConfig: {
				name: 'upgrade',
				alwaysCreate: true,
			},
		});
		const {
			app: { model },
		} = server;
		const { Balance } = model;

		for await (const balance of Balance.find()) {
			['domain', 'httpMaxTask', 'pingMaxTask'].forEach(key => {
				balance.balance[`${key}Enable`] = balance.balance[key].toObject();
				balance.balance[`${key}Enable`].total = balance.balance[key].total;
			});
			await addCountEnable({ balance, model });
			await balance.save();
		}

		process.exit();
	})();
}
